//
//  FileHandle.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "FileHandle.h"

static FileHandle *handle = nil;

@interface FileHandle ()

@property (nonatomic, strong)NSMutableDictionary *plistDic;

@property (nonatomic, strong)NSMutableDictionary *videoDic;

@property (nonatomic , strong ) NSMutableDictionary *newsdic;

@end

@implementation FileHandle

+ (instancetype)fileHandle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[FileHandle alloc]init];
    });
    return handle;
}

- (NSMutableDictionary *)plistDic
{
    if (_plistDic == nil) {
        _plistDic = [[NSMutableDictionary alloc]init];
    }
    return _plistDic;
}

- (NSMutableDictionary *)videoDic
{
    if (_videoDic == nil) {
        _videoDic = [[NSMutableDictionary alloc]init];
    }
    return _videoDic;
}

- (NSMutableDictionary *)newsdic
{
    if (_newsdic == nil) {
        _newsdic = [[NSMutableDictionary alloc]init];
    }
    return _newsdic;
}

#pragma mark>>>>>>>>>>创建 plist
- (void)foundUserName:(NSString *)userName
{
    NSString *username = [NSString stringWithFormat:@"%@.plst", userName];
    NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    NSString *preferences = [libPath stringByAppendingPathComponent:@"/Preferences"];
    self.userPlist = [preferences stringByAppendingPathComponent:username];
    NSLog(@"FileHandle~~~~~~~%@", self.userPlist);
    NSLog(@"filehandle~~~~~~~%d", [[NSFileManager defaultManager]fileExistsAtPath:self.userPlist]);
    if (![[NSFileManager defaultManager]fileExistsAtPath:self.userPlist]) {
        [self.plistDic setObject:self.videoDic forKey:@"videos"];
        [self.plistDic setObject:self.newsdic forKey:@"news"];
        [self.plistDic writeToFile:self.userPlist atomically:YES];
    }else{
        self.plistDic = [NSMutableDictionary dictionaryWithContentsOfFile:self.userPlist];
        self.newsdic = [self.plistDic objectForKey:@"news"];
        [self.plistDic setObject:self.newsdic forKey:@"news"];
        self.videoDic = [self.plistDic objectForKey:@"videos"];
        [self.plistDic setObject:self.videoDic forKey:@"videos"];
        [self.plistDic writeToFile:self.userPlist atomically:YES];
    }
}
// 添加数据
- (void)addVideoInFileDic:(NSDictionary *)dic ID:(NSInteger)ID
{
    NSString *str = [NSString stringWithFormat:@"%ld", (long)ID];
    [self.videoDic setObject:dic forKey:str];
    [self.plistDic setObject:self.videoDic forKey:@"videos"];
    [self.plistDic writeToFile:self.userPlist atomically:YES];
}

- (void)addNewsInFileDic:(NSDictionary *)dic ID:(NSString *)ID
{
    [self.newsdic setObject:dic forKey:ID];
    [self.plistDic setObject:self.newsdic forKey:@"news"];
    [self.plistDic writeToFile:self.userPlist atomically:YES];
}

// 删除数据
- (void)removeVideoForID:(NSInteger)ID
{
    NSString *str = [NSString stringWithFormat:@"%ld", (long)ID];
    [self.videoDic removeObjectForKey:str];
    [self.plistDic setObject:self.videoDic forKey:@"videos"];
    [self.plistDic writeToFile:self.userPlist atomically:YES];
}

- (void)removeNewsForID:(NSString *)ID
{
    [self.newsdic removeObjectForKey:ID];
    [self.plistDic setObject:self.newsdic forKey:@"news"];
    [self.plistDic writeToFile:self.userPlist atomically:YES];
}
// 查找数据
- (NSDictionary *)foundVideoForID:(NSInteger)ID
{
    NSString *str = [NSString stringWithFormat:@"%ld", (long)ID];
    NSArray *keyArray = [self.videoDic allKeys];
    for (NSString *strIn in keyArray) {
        if ([strIn isEqualToString:str]) {
            return [self.videoDic objectForKey:str];
        }
    }
    return nil;
}

- (NSDictionary *)foundNewsForID:(NSString *)ID
{
    NSArray *keyArray = [self.newsdic allKeys];
    for (NSString *str in keyArray) {
        if ([str isEqualToString:ID]) {
            return [self.newsdic objectForKey:str];
        }
    }
    return nil;
}

@end
